const { src, dest, series, parallel, watch } = require('gulp')
const plugins = require('gulp-load-plugins')() // 自动加载插件
const del = require('del') // 文件清除
const bs = require('browser-sync').create()

// --------------- sass处理
// gulp-sass
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({
      outputStyle: 'expanded'
    }))
    .pipe(dest('dist/temp'))
    .pipe(bs.reload({ stream: true })) // 配置自动刷新
}


// --------------- js处理
// gulp-babel @babel/core @babel/preset-env
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({
      presets: ['@babel/preset-env']
    }))
    .pipe(dest('dist/temp'))
    .pipe(bs.reload({ stream: true })) // 配置自动刷新
}


// --------------- 页面模板处理
// gulp-swig
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
const html = () => {
  return src('src/**/*.html', { base: 'src' })
    .pipe(plugins.swig({
      data,
      defaults: { cache: false }
    }))
    .pipe(dest('dist/temp'))
    .pipe(bs.reload({ stream: true })) // 配置自动刷新
}


// --------------- 图片压缩
// gulp-imagemin
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}


// --------------- 字体处理
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}


// --------------- 清除文件
const clean = () => {
  return del(['dist'])
}


// --------------- 公共文件
const extra = () => {
  return src('public/**')
    .pipe(dest('dist'))
}


// --------------- 文件引用处理 - 构建注释
const useRef = () => {
  return src('dist/temp/*.html', { base: 'dist/temp' })
    .pipe(plugins.useref({ searchPath: ['dist/temp', '.'] }))
    // html css js 压缩
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true, // 压缩 内联样式
      minifyJS: true // 压缩 内联script
    })))
    .pipe(dest('dist'))
}


// --------------- 删除缓存目录
const cleanTemp = () => {
  return del(['dist/temp'])
}


// --------------- 开发服务器
const serve = () => {
  // 监听文件变化
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/**/*.html', html)
  watch([
    'public/**',
    'src/assets/fonts/**',
    'src/assets/images/**'
  ], bs.reload)

  bs.init({
    notify: false,
    port: 5000,
    server: {
      baseDir: ['dist/temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const compile = parallel(style, script, html)


// 本地开发
const develop = series(
  clean,
  compile,
  serve
)

// 生产打包
const build = series(
  clean,
  parallel(
    series(compile, useRef, cleanTemp),
    image,
    font,
    extra
  )
)

module.exports = {
  clean,
  develop,
  build
}